/*
 * @Author: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Date: 2025-10-10 10:09:44
 * @LastEditTime: 2025-10-11 17:14:59
 * @LastEditors: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Description: 
 * @FilePath: \lc_-esp32_-s3\main\lc\handheld\bt_app.c
 * 立创实战派ESP32-S3开发板
 * Copyright (c) 2025 by 4D Bios, All Rights Reserved. 
 */

#include "string.h"
#include "handheld.h"
#include "bsp_lvgl.h"
#include "ble_hid_device.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_log.h"
#include "esp_hidd_prf_api.h"
#include "hid_dev.h"

static const char *TAG = "bt_app";

extern bool sec_conn;
extern uint16_t hid_conn_id;

extern lv_obj_t *icon_in_obj; // 应用界面
extern int icon_flag;

static lv_obj_t * label_title; // 标题栏文字;

// 声明使用的字体
LV_FONT_DECLARE(font_alipuhui20);

/**
 * @description: 返回主界面按钮事件处理函数
 * @param {lv_event_t} *e
 * @return {*}
 */
static void btn_back_cb(lv_event_t *e)
{
    lv_obj_del(icon_in_obj); //删除画布
    bt_hid_end();
    icon_flag = 0;
}

/**
 * @description: 音量加按钮回调函数
 * @return {*}
 */
static void btn_volume_up_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if (sec_conn)
    {
        if (code == LV_EVENT_PRESSING)
        {
            esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_UP, true);
            ESP_LOGI(TAG, "UP LV_EVENT_PRESSING");
        }
        else if (code == LV_EVENT_RELEASED)
        {
            esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_UP, false);
            ESP_LOGI(TAG, "UP LV_EVENT_RELEASED");
        }
    }
}

/**
 * @description: 音量减按钮回调函数
 * @return {*}
 */
static void btn_volume_down_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if (sec_conn)
    {
        if (code == LV_EVENT_PRESSING)
        {
            esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_DOWN, true);
            ESP_LOGI(TAG, "DOWN LV_EVENT_PRESSING");
        }
        else if (code == LV_EVENT_RELEASED)
        {
            esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_DOWN, false);
            ESP_LOGI(TAG, "DOWN LV_EVENT_RELEASED");
        }
    }
}

 /**
 * @description: BT应用
 * @return {*}
 */
void bt_app(void)
{
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, 10);
    lv_style_set_opa(&style, LV_OPA_COVER);
    lv_style_set_bg_color(&style, lv_color_hex(0xFFFFFF));
    lv_style_set_border_width(&style, 0);
    lv_style_set_pad_all(&style, 0);
    lv_style_set_width(&style, 320);
    lv_style_set_height(&style, 240);
    
    lvgl_port_lock(0);

    // 创建一个界面
    icon_in_obj = lv_obj_create(lv_scr_act());
    lv_obj_add_style(icon_in_obj, &style, 0);

    // 创建标题背景
    lv_obj_t *obj_title = lv_obj_create(icon_in_obj);
    lv_obj_set_size(obj_title, 320, 40);
    lv_obj_set_align(obj_title, LV_ALIGN_TOP_MID);
    lv_obj_set_style_pad_all(obj_title, 0, 0);  // 设置间隙
    lv_obj_set_style_bg_color(obj_title, lv_color_hex(0xb87fa8), 0);

    label_title = lv_label_create(obj_title);
    lv_label_set_text(label_title, "蓝牙控制器");
    lv_obj_set_style_text_font(label_title, &font_alipuhui20, 0);
    lv_obj_set_style_text_color(label_title, lv_color_hex(0xffffff), 0);
    lv_obj_set_align(label_title, LV_ALIGN_CENTER);

    // 返回按钮
    lv_obj_t *btn_back = lv_btn_create(obj_title);
    lv_obj_align(btn_back, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_size(btn_back, 60, 30);
    lv_obj_set_style_border_width(btn_back, 0, 0); // 设置边框宽度
    lv_obj_set_style_pad_all(btn_back, 0, 0);  // 设置间隙
    lv_obj_set_style_bg_opa(btn_back, LV_OPA_0, 0);
    lv_obj_set_style_shadow_opa(btn_back, LV_OPA_TRANSP, LV_PART_MAIN); // 阴影透明
    lv_obj_add_event_cb(btn_back, btn_back_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *label_back = lv_label_create(btn_back);
    lv_label_set_text(label_back, LV_SYMBOL_LEFT);
    lv_obj_set_style_text_font(label_back, &lv_font_montserrat_20, 0);
    lv_obj_align(label_back, LV_ALIGN_CENTER, -10, 0);

    //音量加按钮
    lv_obj_t *btn_volume_up = lv_btn_create(icon_in_obj);
    lv_obj_align(btn_volume_up, LV_ALIGN_CENTER, -50, 0);
    lv_obj_set_size(btn_volume_up, 80, 80);
    lv_obj_add_event_cb(btn_volume_up, btn_volume_up_cb, LV_EVENT_ALL, NULL);

    lv_obj_t *label_volume_up = lv_label_create(btn_volume_up);
    lv_label_set_text(label_volume_up, LV_SYMBOL_VOLUME_MAX);
    lv_obj_set_style_text_font(label_volume_up, &lv_font_montserrat_20, 0);
    lv_obj_align(label_volume_up, LV_ALIGN_CENTER, 0, 0);

    //音量减按钮
    lv_obj_t *btn_volume_down = lv_btn_create(icon_in_obj);
    lv_obj_align(btn_volume_down, LV_ALIGN_CENTER, 50, 0);
    lv_obj_set_size(btn_volume_down, 80, 80);
    lv_obj_add_event_cb(btn_volume_down, btn_volume_down_cb, LV_EVENT_ALL, NULL);

    lv_obj_t *label_volume_down = lv_label_create(btn_volume_down);
    lv_label_set_text(label_volume_down, LV_SYMBOL_VOLUME_MID);
    lv_obj_set_style_text_font(label_volume_down, &lv_font_montserrat_20, 0);
    lv_obj_align(label_volume_down, LV_ALIGN_CENTER, 0, 0);

    lvgl_port_unlock();

    bt_hid_start();

    icon_flag = 6;
}